package com.kehutong.material.admin;

import org.coraframework.authz.HasPermission;
import org.coraframework.bean.valid.ValidResult;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import com.kehutong.material.entity.Verbal;

@MvcAction
@ReqMapping("/admin/crm/verbal")
public class VerbalController {
	
	@Inject
	private JdbcSession jdbcSession;

    @HasPermission("crm:verbal:view")
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) throws Exception {
        return jdbcSession.findPage(Verbal.class)
        		.eq(jsonObject, "id", "pid")
            	.eq("deleted", false)
                .like(jsonObject, "name")
                .order("createTime", "desc")
                .page(jsonObject)
                .exe();
    }

    @HasPermission("crm:verbal:view")
    @ReqMapping("/get")
    public Object get(Verbal item) {
    	return item;
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("crm:verbal:edit")
    @ReqMapping("/save")
    public Object save(Verbal item, ValidResult result) {
    	if (result.hasError("userId")) {
    		return Result.error(0, result.getErrorMessage());
    	}
    	
    	jdbcSession.insert(item);
    	return Result.success();
    }

    @Bean(copy=true)
    @HasPermission("crm:verbal:edit")
    @ReqMapping("/update")
    public Object update(Verbal item, ValidResult result) {
    	if (result.hasError("userId")) {
    		return Result.error(0, result.getErrorMessage());
    	}
    	
    	jdbcSession.updateById(item);
    	return Result.success();
    }

    @HasPermission("crm:verbal:edit")
    @ReqMapping("/delete")
    public Object delete(Verbal item) throws Exception {
    	if (Objects.nonNull(item)) {
    		item.setDeleted(true);
    		jdbcSession.updateById(item);
    	}
    	
    	return Result.success();
    }
}
